<?php

namespace app\service;

use app\common\BaseService;
use app\model\Store as StoreModel;
use think\facade\Db;

class StoreService extends BaseService
{
  public static function getInstance(): StoreService
  {
    return parent::getInstance();
  }

  public static function getModel(): StoreModel
  {
    return StoreModel::getInstance();
  }

  public function getDataByCondition($conditions) {
    return self::getModel()->getDataByCondition($conditions);
  }

  /*
   * 通过condition获取Store
   */
  public function getStoreByCondition($conditions, $order = ['s.base' => 'asc','s.id' => 'desc']) {
    $query = self::getModel()
      ->alias('s')
      ->field('s.*, u.basename, c.category_name')
      ->join('user u', 'u.base = s.base', 'LEFT')
      ->join('category c', 'c.id = s.category_id', 'LEFT');
    foreach ($conditions as $item) {
      list($field, $value, $op) = $item;
      if (!$op) {
        $op = '=';
      }
      $query = $query->where('s.'.$field, $op, $value);
    }
    $query = $query->order($order);
    $result = $query->select()->toArray();
    return $result;
  }
}